package com.yugao.fintech.antelope.pay.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.pay.common.assembly.TradeDataAssembly;
import com.yugao.fintech.antelope.pay.common.mapper.TradeDataMapper;
import com.yugao.fintech.antelope.pay.common.service.TradeDataService;
import com.yugao.fintech.antelope.pay.model.entity.TradeData;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.payorder.TradeDataQO;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.payorder.TradeDataRS;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


@Service
public class TradeDataServiceImpl extends ServiceImpl<TradeDataMapper, TradeData> implements TradeDataService {
    @Autowired
    private TradeDataAssembly tradeDataAssembly;

    @Override
    public TradeDataRS getTradeData(TradeDataQO req) {
        if (StringUtils.isAllEmpty(req.getMchOrderNo(), req.getPayOrderNo())) {
            throw new BizException("payOrderNo 和 mchOrderNo 不能同时为空");
        }

        LambdaQueryWrapper<TradeData> lqw = TradeData.lqw()
                .eq(TradeData::getMchNo, req.getMchNo())
                .eq(TradeData::getAppId, req.getAppId())
                .eq(StringUtils.isNotEmpty(req.getMchOrderNo()), TradeData::getMchOrderNo, req.getMchOrderNo())
                .eq(StringUtils.isNotEmpty(req.getPayOrderNo()), TradeData::getPayOrderNo, req.getPayOrderNo());
        return tradeDataAssembly.toTradeDataResp(this.getOne(lqw));
    }
}
